package com.ruoyi.web.controller.his;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.his.domain.Doctor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.his.domain.Patient;
import com.ruoyi.his.service.IPatientService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 病患信息Controller
 *
 * @author hzr
 * @date 2024-10-16
 */
@RestController
@RequestMapping("/his/patient")
public class PatientController extends BaseController
{
    @Autowired
    private IPatientService patientService;
    @GetMapping("/dailyNewPatients")
    public AjaxResult getDailyNewPatients() {
        // 获取今天和七天前的日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String endDate = sdf.format(new Date());
        Date sevenDaysAgo = new Date(System.currentTimeMillis() - 7L * 24 * 60 * 60 * 1000);
        String startDate = sdf.format(sevenDaysAgo);

        List<Map<String, Object>> data = patientService.getDailyNewPatients(startDate, endDate);
        return AjaxResult.success(data);
    }
    /**
     * 查询病患信息列表
     */
    @PreAuthorize("@ss.hasPermi('his:patient:list')")
    @GetMapping("/list")
    public TableDataInfo list(Patient patient)
    {
        startPage();
        List<Patient> list = patientService.selectPatientList(patient);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('his:patient:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll()
    {
        Patient patient = new Patient();
        patient.setStatus("0");
        List<Patient> list = patientService.selectPatientList(patient);
        return getDataTable(list);
    }

    /**
     * 导出病患信息列表
     */
    @PreAuthorize("@ss.hasPermi('his:patient:export')")
    @Log(title = "病患信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Patient patient)
    {
        List<Patient> list = patientService.selectPatientList(patient);
        ExcelUtil<Patient> util = new ExcelUtil<Patient>(Patient.class);
        util.exportExcel(response, list, "病患信息数据");
    }

    /**
     * 获取病患信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:patient:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(patientService.selectPatientById(id));
    }

    /**
     * 新增病患信息
     */
    @PreAuthorize("@ss.hasPermi('his:patient:add')")
    @Log(title = "病患信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Patient patient)
    {
        return toAjax(patientService.insertPatient(patient));
    }

    /**
     * 修改病患信息
     */
    @PreAuthorize("@ss.hasPermi('his:patient:edit')")
    @Log(title = "病患信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Patient patient)
    {
        return toAjax(patientService.updatePatient(patient));
    }

    /**
     * 删除病患信息
     */
    @PreAuthorize("@ss.hasPermi('his:patient:remove')")
    @Log(title = "病患信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(patientService.deletePatientByIds(ids));
    }
}
